<?php


namespace app\service;


use think\facade\Db;

class WxTplService
{

    public function sendVerifyMsg($strangerApplyInfo,$staff){

        //获取全局token
        $token = $this->getToken();
        $template_id="6s2cLvGshWcEUmneVHT1MmEHLfoxyTBjn_q3XOk9REY";
        $url="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$token; //模板信息请求地址
        //https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN
        //发送的模板信息(微信要求json格式，这里为数组（方便添加变量）格式，然后转为json)
        $post_data = array(
            "touser"=>$staff["openid"], //推送给谁,openid
            "template_id"=>$template_id, //微信后台的模板信息id
            "page"=>"pages/home/home",//pages/admin/stranger/index
            "data"=> array(
                "thing9" => array(
                    "value"=>$strangerApplyInfo["community_name"],
                    "color"=>"#173177"
                ),
                "thing5" => array(
                    "value"=>$strangerApplyInfo["name"],//姓名
                    "color"=>"#173177"
                ),
                "phone_number6" => array(
                    "value"=>$strangerApplyInfo["mobile"],//用户名称
                    "color"=>"#173177"
                ),

                "thing1" => array(
                    "value"=>"外卖",//审核类型
                    "color"=>"#173177"
                ),
                "phrase7"=>array(
                    "value"=>"待审核",
                    "color"=>"#ff8c00"
                ),
            )
        );
        //将上面的数组数据转为json格式
        $post_data = json_encode($post_data);
        $data=$this->curl_post($url,$post_data);
        //var_dump($data);exit;
        $info["template_id"]=$template_id;
        $info["errcode"]=$data["errcode"];
        $info["errmsg"]=$data["errmsg"];
        if($data["errcode"]==0){
            $info["msgid"]=$data["msgid"];
        }

        $info["stranger_apply_id"]=$strangerApplyInfo["id"];
        $info["name"]=$strangerApplyInfo["name"];
        $info["community_id"]=$strangerApplyInfo["community_id"];
        $info["device_id"]=$strangerApplyInfo["device_id"];
        $info["device_name"]=$strangerApplyInfo["device_name"];

        $info["staff_id"]=$staff["id"];
        $info["staff_name"]=$staff["name"];
        $info["staff_mobile"]=$staff["mobile"];
        $info["create_time"]=date("Y-m-d H:i:s");
        Db::table("log_wechat_msg")->insert($info);

        return $data;
    }
    /**
     * @param $url
     * @param array $data
     * @return mixed
     * curl请求
     */
    function curl_post($url , $post_data=''){

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url); //设置发送数据的网址
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设置有返回值，0，直接显示
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); //禁用证书验证
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post请求发送的数据包
        //接收执行返回的数据
        $data = curl_exec($ch);
        //关闭句柄
        curl_close($ch);
        $data = json_decode($data,true); //将json数据转成数组
        return $data;
    }
    public function getToken(){
        $redis=new \Redis;
        $redis->connect("127.0.0.1",6379);
        $redis->auth(env('APP.REDIS_PASSWORD'));
        if($redis->exists("wx_access_token")){
            return $redis->get("wx_access_token");
        }else{
            $appid=env('APP.WECHAT_MINI_APPID');
            $secret=env('APP.WECHAT_MINI_SECRET');
            $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
            $result=file_get_contents($url);

            $result=json_decode($result,true);

            $redis->set("wx_access_token", $result["access_token"], 7200);
            return $result["access_token"];
        }

    }


}